<template>
  <div class="edit-bg">
    <div class="edit-card">
      <button class="return-btn blue" type="button" @click="onCancel">返回</button>
      <div class="edit-title">修改诊疗项目</div>
      <form @submit.prevent="onSubmit">
        <div class="edit-form-row">
          <label class="edit-label">项目类别：</label>
          <input v-model="category" class="edit-input" placeholder="请输入项目类别" />
        </div>
        <div class="edit-form-row">
          <label class="edit-label">项目编码：</label>
          <input v-model="code" class="edit-input" placeholder="请输入项目编码" />
        </div>
        <div class="edit-form-row">
          <label class="edit-label">项目明细编码：</label>
          <input v-model="detailCode" class="edit-input" placeholder="请输入项目明细编码" />
        </div>
        <div class="edit-form-row">
          <label class="edit-label">项目名称：</label>
          <input v-model="name" class="edit-input" placeholder="请输入项目名称" />
        </div>
        <div class="edit-form-row">
          <label class="edit-label">项目说明：</label>
          <input v-model="desc1" class="edit-input" placeholder="请输入项目说明" />
        </div>
        <div class="edit-form-row">
          <label class="edit-label">除外内容：</label>
          <input v-model="exclude" class="edit-input" placeholder="请输入除外内容" />
        </div>
        <div class="edit-form-row">
          <label class="edit-label">计价单位：</label>
          <input v-model="unit" class="edit-input" placeholder="请输入计价单位" />
        </div>
        <div class="edit-form-row">
          <label class="edit-label">价格：</label>
          <button type="button" class="price-btn" @click="decreasePrice">-</button>
          <input v-model.number="price" class="edit-input price-input" style="width: 120px; text-align: center; margin: 0 8px;" />
          <button type="button" class="price-btn" @click="increasePrice">+</button>
        </div>
        <div class="edit-form-row">
          <label class="edit-label">备注：</label>
          <textarea v-model="remark" class="edit-input remark-textarea" placeholder="请输入备注"></textarea>
        </div>
        <div class="edit-actions">
          <button class="edit-btn" type="submit">修改</button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()

const category = ref('')
const code = ref('')
const detailCode = ref('')
const name = ref('')
const desc1 = ref('')
const exclude = ref('')
const unit = ref('')
const price = ref(0)
const remark = ref('')

onMounted(() => {
  if (route.params.id && route.params.id !== 'new') {
    // 修改，读取 localStorage
    const editProject = JSON.parse(localStorage.getItem('editProject') || '{}')
    category.value = editProject.category || ''
    code.value = editProject.code || ''
    detailCode.value = editProject.detailCode || ''
    name.value = editProject.name || ''
    desc1.value = (editProject.target ? editProject.target + ' ' : '') + (editProject.content || '')
    exclude.value = editProject.exclude || ''
    unit.value = editProject.unit || ''
    price.value = editProject.price || 0
    remark.value = editProject.remark || ''
  } else {
    // 新增，全部置空
    category.value = ''
    code.value = ''
    detailCode.value = ''
    name.value = ''
    desc1.value = ''
    exclude.value = ''
    unit.value = ''
    price.value = 0
    remark.value = ''
  }
})

function increasePrice() {
  price.value = (Number(price.value) || 0) + 1
}
function decreasePrice() {
  price.value = Math.max(0, (Number(price.value) || 0) - 1)
}
function onSubmit() {
  alert('保存成功！')
  router.push('/projects')
}
function onCancel() {
  router.push('/projects')
}
</script>

<style scoped>
.edit-bg {
  min-height: 100vh;
  background: #f5f6fa;
  width: 100%;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
  padding: 0;
}
.edit-card {
  background: #fff;
  border-radius: 0;
  box-shadow: none;
  padding: 0;
  width: 100%;
  min-width: 0;
  max-width: none;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  position: relative;
}
.edit-title {
  font-size: 28px;
  font-weight: bold;
  color: #222;
  margin: 32px 0 32px 64px;
  text-align: left;
  letter-spacing: 1px;
}
.edit-form-row {
  display: flex;
  align-items: center;
  margin-bottom: 18px;
  padding: 0;
}
.edit-label {
  width: 160px;
  color: #34495e;
  font-size: 16px;
  text-align: right;
  margin-right: 12px;
  position: relative;
}
.edit-input {
  flex: 1;
  height: 36px;
  border: 1px solid #d0d5dd;
  border-radius: 4px;
  padding: 0 12px;
  font-size: 15px;
  width: 100%;
  max-width: 600px;
}
.price-input {
  width: 120px !important;
  min-width: 60px;
  max-width: 160px;
  display: inline-block;
}
.price-btn {
  width: 32px;
  height: 32px;
  border: 1px solid #d0d5dd;
  background: #f5f6fa;
  color: #34495e;
  border-radius: 4px;
  font-size: 20px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 0 2px;
  transition: background 0.2s;
}
.price-btn:hover {
  background: #eaf3ff;
}
.edit-actions {
  margin: 32px 0 0 64px;
  display: flex;
  gap: 18px;
}
.edit-btn {
  background: #409eff;
  color: #fff;
  border: none;
  border-radius: 4px;
  height: 36px;
  padding: 0 32px;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.return-btn {
  position: absolute;
  top: 24px;
  right: 32px;
  background: #409eff;
  color: #fff;
  border: none;
  border-radius: 4px;
  height: 36px;
  padding: 0 32px;
  font-size: 16px;
  cursor: pointer;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
}
.remark-textarea {
  min-height: 108px;
  height: 108px;
  resize: vertical;
  font-family: inherit;
}
</style> 